<?

class UploadFile extends Button  {

    function render($sel) {
        return $this->renderDialogButton('uploadfile','Upload file','Upload file to server');
    }

    function action($sel) {
        $DENIED_EXTENSIONS = Config::getValue('limits','DENIED_EXTENSIONS');
        if (is_uploaded_file($_FILES['newfile']['tmp_name']))  {
            $ext = substr($_FILES['newfile']['name'],strrpos($_FILES['newfile']['name'],'.')+1);
            if (!in_array($ext,$DENIED_EXTENSIONS)) {
                move_uploaded_file($_FILES['newfile']['tmp_name'],$sel->getCurrentDirName().basename($_FILES['newfile']['name']));
                $f = new File($sel->getCurrentDirName().basename($_FILES['newfile']['name']));
                header('Location: '.Config::getMsgLink($_GET['path'],basename($_FILES['newfile']['name']),MESSAGE_INFO,'File <b>'.basename($f->name).'</b> ('.$f->getSizeF().') was uploaded successfully'));
                exit;
            } else {
                header('Location: '.Config::getMsgLink($_GET['path'],'',MESSAGE_ERROR,'File extension <b>'.$ext.'</b> is forbidden to use!'));
            }
        }

    }

    function renderDialog($sel) {
        return '<form enctype="multipart/form-data" method="POST" action="'.Config::getLink($_GET['path'],'','action=uploadfile').'"><b>File name: </b> <input type="file" name="newfile"><input type="submit" value="Upload"></form>';
    }

}

?>